package com.demo.kit;

import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import javax.imageio.ImageIO;

public class ImageUtil {
	
	/**
	 * 裁剪图片
	 * @param prevImage
	 * @param is		输入流
	 * @param imgPath 	图片名称：aaa.jpg
	 * @param savePath	图片目录绝对路径：webapps/serviceImg/0000-00-00
	 * @param maxSize	最大尺寸，当原图尺寸大于此值后，将会保存一张尺寸为maxSize的图片
	 * @param minSize	最小尺寸，当原图尺寸大于此值后，将会保存一张尺寸为minSize的图片
	 * @throws IOException
	 */
    public static void resizeImage(BufferedImage prevImage, InputStream is, String imgPath ,
    		String savePath, int maxSize, int minSize) throws IOException {  
        double width = prevImage.getWidth();  
        double height = prevImage.getHeight(); 
        double size ;
        size = width < maxSize ? width : maxSize ;	//原图宽度小于800，继续保存原图，否则，裁剪为800宽度
//        size = width < 800 ? width : 800 ;	//原图宽度小于800，继续保存原图，否则，裁剪为800宽度
        executeResize(is,prevImage, width, height, imgPath, savePath, size, "jpg", maxSize);
//        executeResize(is,prevImage, width, height, imgPath, savePath, size, "jpeg", 800);
        
        size = width > minSize ? minSize : width ;	//原图宽度大于200，裁剪为200宽度,否则继续保存原图
        executeResize(is, prevImage, width, height, imgPath, savePath, size, "jpg", minSize);
//        size = width > 200 ? 200 : width ;	//原图宽度大于200，裁剪为200宽度,否则继续保存原图
//        executeResize(is, prevImage, width, height, imgPath, savePath, size, "jpeg", 200);
    }  
    /**
     * 处理图片，并保存
     * @param is
     * @param prevImage
     * @param width
     * @param height
     * @param ImgPath
     * @param savePath
     * @param size
     * @param format
     * @param name
     */
	private static void executeResize(InputStream is, BufferedImage prevImage,double width, 
			double height,String ImgPath ,String savePath, double size, String format, int name){
		
	  	double percent = size/width;  
        OutputStream os;
		try {
//			System.out.println(savePath+"\\"+name+"_"+ImgPath);
			os = new FileOutputStream(savePath+"\\"+name+"_"+ImgPath);	//实际存放文件的路径
			int newWidth = (int)(width * percent);  
			int newHeight = (int)(height * percent);  
			BufferedImage image = new BufferedImage(newWidth, newHeight, BufferedImage.TYPE_INT_BGR);  
			Graphics graphics = image.createGraphics();  
			graphics.drawImage(prevImage, 0, 0, newWidth, newHeight, null);  
			ImageIO.write(image, format, os);
			os.flush();  
			is.close();  
			os.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} 
		catch (IOException e) {
			e.printStackTrace();
		}
	}
	/**
	 * 图片的复制
	 * @param srcPath	原路径
	 * @param descPath	新路径
	 */
	public static void copyFile(String srcPath, String descPath) { 
	       try { 
//	           int bytesum = 0; 
	           int byteread = 0; 
	           File oldfile = new File(srcPath); 
	           if (oldfile.exists()) { //文件存在时 
	               InputStream inStream = new FileInputStream(srcPath); //读入原文件 
	               FileOutputStream fs = new FileOutputStream(descPath); 
	               byte[] buffer = new byte[1024]; 
	               while ( (byteread = inStream.read(buffer)) != -1) { 
//	                   bytesum += byteread; //字节数 文件大小 
//	                   System.out.println(bytesum); 
	                   fs.write(buffer, 0, byteread); 
	               } 
	               inStream.close(); 
	               fs.close();
	           } 
	       } 
	       catch (Exception e) { 
	           System.out.println("复制单个文件操作出错"); 
	           e.printStackTrace(); 
	       } 
	   } 
}
